package com.xy.xxjob;

import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.handler.annotation.XxlJob;
import com.xy.config.CommonSql;
import com.xy.config.ScreenRedis;
import com.xy.service.ScreenService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springblade.core.redis.cache.BladeRedis;
import org.springblade.core.tool.utils.ConcurrentDateFormat;
import org.springblade.core.tool.utils.DateUtil;
import org.springblade.core.tool.utils.Func;
import org.springframework.data.redis.connection.RedisClusterConnection;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.RedisNode;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Component
@Slf4j
@AllArgsConstructor
public class ScreenJob {

	private final ScreenService screenService;
	private final BladeRedis bladeRedis;

	/**
	 * 查询每日入场人次
	 */
	@XxlJob("jobEntrancePeopleTotal")
	public ReturnT<String> jobEntrancePeopleTotal(String param) throws Exception {
		String sql = CommonSql.jobEntrancePeopleTotal;
		LinkedHashMap<String, Object> linkedHashMaps = screenService.getdynamicQuery(sql);
		bladeRedis.hSet(ScreenRedis.SCREEN_PEOPLE_ORDER_TOTAL, ConcurrentDateFormat.of("MM月dd日").format(new Date()), linkedHashMaps);
		return ReturnT.SUCCESS;
	}

	/**
	 * 查询每日入场人数
	 */
	@XxlJob("jobEntrancePeopleOrderTotal")
	public ReturnT<String> jobEntrancePeopleOrderTotal(String param) throws Exception {
		String sql = CommonSql.jobEntrancePeopleTotalGroupBy;
		LinkedHashMap<String, Object> linkedHashMaps = screenService.getdynamicQuery(sql);
		bladeRedis.hSet(ScreenRedis.SCREEN_PEOPLE_TOTAL, ConcurrentDateFormat.of("MM月dd日").format(new Date()), linkedHashMaps);
		return ReturnT.SUCCESS;
	}

	/**
	 * 查询每日入场证件类型人数
	 */
	@XxlJob("jobEntrancePeopleOrderTotalType")
	public ReturnT<String> jobEntrancePeopleOrderTotalType(String param) throws Exception {
		log.info("查询每日入场证件类型人数{}", param);
		String sql = CommonSql.jobEntrancePeopleTotalGroupByType;
		List<LinkedHashMap<String, Object>> linkedHashMaps = screenService.getListdynamicQuery(sql);
//		Map<Object, List<LinkedHashMap<String, Object>>> objectListMap = linkedHashMaps.stream().collect(Collectors.groupingBy(o -> o.get("exhibitionId")));
//		objectListMap.entrySet().forEach(o -> {
//			Object key = o.getKey();
//			List<LinkedHashMap<String, Object>> hashMapList = o.getValue();
		bladeRedis.hSet(ScreenRedis.SCREEN_PEOPLE_TYPE_TOTAL, ConcurrentDateFormat.of("MM月dd日").format(new Date()), linkedHashMaps);
//		});
		return ReturnT.SUCCESS;
	}

	/**
	 * 年龄段
	 */
	@XxlJob("jobEntranceAge")
	public ReturnT<String> jobEntranceAge(String param) throws Exception {
		String sql = CommonSql.jobEntranceAge;
		List<LinkedHashMap<String, Object>> linkedHashMaps = screenService.getListdynamicQuery(sql);
		bladeRedis.hSet(ScreenRedis.SCREEN_PEOPLE_AGE, ConcurrentDateFormat.of("MM月dd日").format(new Date()), linkedHashMaps);
		return ReturnT.SUCCESS;
	}

	/**
	 * 黑名单
	 */
	@XxlJob("jobEntrancebackList")
	public ReturnT<String> jobEntrancebackList(String param) throws Exception {
		String sql = CommonSql.jobEntrancebackList;
		List<LinkedHashMap<String, Object>> linkedHashMaps = screenService.getListdynamicQuery(sql);
		bladeRedis.hSet(ScreenRedis.SCREEN_BACK_LIST, ConcurrentDateFormat.of("MM月dd日").format(new Date()), linkedHashMaps);
		return ReturnT.SUCCESS;
	}

	/**
	 * 时间段
	 */
	@XxlJob("jobEntrancePeopleTotalTimeSlot")
	public ReturnT<String> jobEntrancePeopleTotalTimeSlot(String param) throws Exception {
		String sql = CommonSql.jobEntrancePeopleTotalTimeSlot;
		List<LinkedHashMap<String, Object>> linkedHashMaps = screenService.getListdynamicQuery(sql);
		bladeRedis.hSet(ScreenRedis.SCREEN_PEOPLE_TIME, ConcurrentDateFormat.of("MM月dd日").format(new Date()), linkedHashMaps);
		return ReturnT.SUCCESS;
	}

	/**
	 * 查询每日入场人次
	 */
	@XxlJob("jobEntrancePeopleBc")
	public ReturnT<String> jobEntrancePeopleBc(String param) throws Exception {
		Date minusDays = DateUtil.minusDays(new Date(), Func.toLong(param));

		Date plusDays = DateUtil.plusDays(minusDays, 1);
		String endTime =  DateUtil.format(plusDays,DateUtil.PATTERN_DATETIME);

		String time = DateUtil.format(minusDays,DateUtil.PATTERN_DATETIME);
		String sql = "SELECT b.exhibition_id exhibitionId,COUNT(0) value,now() date from ( " +
			" SELECT * from oa_general_record  where exhibition_id in ( " +
			" SELECT id  FROM oa_exhibition where close_status = 0) )a " +
			" LEFT JOIN oa_viewer b on a.viewer_id = b.id " +
			" where a.pass_time  >= STR_TO_DATE( '"+time+"', '%Y-%m-%d' ) and a.pass_time  < STR_TO_DATE( '"+endTime+"', '%Y-%m-%d' )";
		LinkedHashMap<String, Object> linkedHashMaps = screenService.getdynamicQuery(sql);
		bladeRedis.hSet(ScreenRedis.SCREEN_PEOPLE_ORDER_TOTAL, ConcurrentDateFormat.of("MM月dd日").format(minusDays), linkedHashMaps);

		String sql1 = "SELECT count(0) value ,now() date, a.exhibitionId from ( "  +
			" SELECT b.exhibition_id exhibitionId,COUNT(0) num,now() date from ( " +
			" SELECT * from oa_general_record  where exhibition_id in ( " +
			" SELECT id  FROM oa_exhibition where close_status = 0) )a " +
			" LEFT JOIN oa_viewer b on a.viewer_id = b.id " +
			" where a.pass_time   >= STR_TO_DATE(  '"+time+"', '%Y-%m-%d' )  and a.pass_time  < STR_TO_DATE(  '"+endTime+"', '%Y-%m-%d' )  GROUP BY  b.id_card ) a";
		LinkedHashMap<String, Object> linkedHash = screenService.getdynamicQuery(sql1);
		bladeRedis.hSet(ScreenRedis.SCREEN_PEOPLE_TOTAL, ConcurrentDateFormat.of("MM月dd日").format(minusDays), linkedHash);


		String sql2 ="SELECT b.quantum time ,IFNULL(a.num,0) num from ( SELECT DATE_FORMAT( create_time, '%H' )time ,COUNT(DATE_FORMAT( create_time, '%H' )) num FROM  oa_general_record  WHERE create_time   >= STR_TO_DATE('"+time+"','%Y-%m-%d') and create_time   < STR_TO_DATE('"+endTime+"','%Y-%m-%d')" +
			" and exhibition_id in (SELECT id from oa_exhibition where close_status =0) GROUP BY DATE_FORMAT( create_time, '%H' )  )a   RIGHT  JOIN oa_quantum b ON a.time = b.quantum ";
		List<LinkedHashMap<String, Object>> linkedHashMaps1 = screenService.getListdynamicQuery(sql2);
		bladeRedis.hSet(ScreenRedis.SCREEN_PEOPLE_TIME, ConcurrentDateFormat.of("MM月dd日").format(minusDays), linkedHashMaps1);

		return ReturnT.SUCCESS;
	}
}
